<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/22
 * Time: 8:59
 */
namespace Admin\Controller;
use Think\Controller;

class HouseController extends Controller
{
    public function json(){
        if(!S('house')){
            $data = D('Room')->relation(true)->select();
//            echo D('Room')->getLastSql();
//            $this->ajaxReturn($data);
            foreach($data as $k=>$v){
                $c[$v['community_id']]['name'] = $v['community']['name'];

                $z[$v['zone_id']]['name'] = $v['zone']['name'];
                $z[$v['zone_id']]['pid'] = $v['community_id'];

                $b[$v['building_id']]['name'] = $v['building']['name'];
                $b[$v['building_id']]['pid'] = $v['zone_id'];
                $b[$v['building_id']]['ppid'] = $v['community_id'];

                $u[$v['unit_id']]['name'] = $v['unit']['name'];
                $u[$v['unit_id']]['pid'] = $v['building_id'];
                $u[$v['unit_id']]['ppid'] = $v['zone_id'];
                $u[$v['unit_id']]['pppid'] = $v['community_id'];

                $r[$v['id']]['name'] = $v['name'];
                $r[$v['id']]['pid'] = $v['unit_id'];
                $r[$v['id']]['ppid'] = $v['building_id'];
                $r[$v['id']]['pppid'] = $v['zone_id'];
                $r[$v['id']]['ppppid'] = $v['community_id'];
            }

            foreach($c as $k=>$v){
                $result[$k]['community_id'] = $k;
                $result[$k]['community_name'] = $v['name'];
            }

            foreach($z as $k=>$v){
                $result[$v['pid']]['data'][$k]['zone_id'] = $k;
                $result[$v['pid']]['data'][$k]['zone_name'] = $v['name'] ? $v['name'] : '本园区';
            }

            foreach($b as $k=>$v){
                $result[$v['ppid']]['data'][$v['pid']]['data'][$k]['building_id'] = $k;
                $result[$v['ppid']]['data'][$v['pid']]['data'][$k]['building_name'] = $v['name'];
            }

            foreach($u as $k=>$v){
                $result[$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['data'][$k]['unit_id'] = $k;
                $result[$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['data'][$k]['unit_name'] = $v['name'] ? $v['name'] : '本单元';
            }

            foreach($r as $k=>$v){
                if($result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]){
                    $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['data'][$k]['room_id'] = $k;
                    $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['data'][$k]['room_name'] = $v['name'];
                }else{
                    $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['unit_id'] = $v['pid'];
                    $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['unit_name'] = '0';
                }
            }
            S('house',$result);
        }
        header('Access-Control-Allow-Origin: *');
        $res['code'] = 200;
        $res['data'] = S('house');
        $this->ajaxReturn($res);
    }
}